package com.baizhi.cmfz.controller;


import com.baizhi.cmfz.annotation.LogAnnotation;
import com.baizhi.cmfz.dao.ArticleDao;
import com.baizhi.cmfz.entiy.Article;
import com.baizhi.cmfz.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("article")
public class ArticleController {

    @Autowired
    private ArticleService service;

    //分页与全文检索
    @RequestMapping("getArticleByPage")
    @ResponseBody
    @LogAnnotation("分页查询上师文章")
    public Map getArticleByPage(int page,int rows,String keyword){
       // System.out.println("page"+page+"rows"+rows+"value"+keyword);
        return service.getArticleByPage(page,rows,keyword);
    }



    //重置索引
    @RequestMapping("resetIndex")
    @ResponseBody
    public Map resetIndex(){
        return service.resetIndex();
    }
    //添加
    @RequestMapping("add")
    @ResponseBody
    public Map add(Article article){
        HashMap map = new HashMap();

        try {
            service.add(article);
            map.put("isAdd",true);
            //  重置索引
            service.resetIndex();
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isAdd",false);
        }

        return map;
    }

    @RequestMapping("update")
    @ResponseBody
    public Map update(Article article){

        HashMap map = new HashMap();

        try {
            service.update(article);
            map.put("isUpdate",true);
            //  重置索引
            service.resetIndex();
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isUpdate",false);
        }

        return map;
    }

}
